package com.big.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.big.app.dao.UrlsDao;
import com.big.app.entity.Urls;
import com.big.app.service.intf.UrlsService;
import com.big.app.vo1.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class UrlsImpl implements UrlsService {
    @Resource
    private UrlsDao dao;

    @Override
    public R all() {
        boolean b = true;
        String gzusers="123";
        int i = 1;
        ArrayList<Integer> list1 = new ArrayList<>();
        List<Urls> list = dao.selectList(null);
        for (Urls s:list) {
            list1.add(s.getId());
        }
        Random random = new Random();
        while (b){
             i = random.nextInt(list.size());
           if (list1.contains(i)){
               b=false;
           }else {
               b = true;
           }
        }
        Urls urls = dao.selectOne(new QueryWrapper<Urls>().eq("id", i));

        List<Urls> list2 = dao.a(gzusers);

        for (Urls s:list2) {
            if(s.getUser().equals(urls.getUser())){
                urls.setGztag(1);
            }
        }

        System.out.println(urls);
        return new R(200,"ok",urls);
    }

    @Override
    public R update(Urls urls) {

        System.out.println(urls.getLikecount()+""+urls.getId());
//        Urls urls = new Urls();
//        urls.setId(id);
//        urls.setTag(tag);
//        urls.setLikecount(likecount);
        int i = dao.updateById(urls);
        System.out.println(i);
        return new R(200,"ok",null);
    }

    @Override
    public R updates(Urls urls) {
        System.out.println(urls.getGztag()+""+urls.getId());

        int i = dao.updateById(urls);
        System.out.println(i);
        return new R(200,"ok",null);
    }

    @Override
    public R updatee(Urls urls) {
        System.out.println(urls.getSctag()+""+urls.getId());

        int i = dao.updateById(urls);
        System.out.println(i);
        return new R(200,"ok",null);
    }

    @Override
    public R xhlist() {
        return new R(200,"ok",dao.selectList(new QueryWrapper<Urls>().eq("tag",1)));
    }

    @Override
    public R sousuo(Urls urls) {
        String user = urls.getUser();
        List<Urls> list = dao.selectList(new QueryWrapper<Urls>().eq("user", user));
        System.out.println(list);
        if (list == null){
            return new R(400,"false",null);
        }else {
            return new R(200,"ok",list);
        }
    }

}
